package com.xc.erp.modules.erp.controller.supplier;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xc.erp.common.annotation.Log;
import com.xc.erp.common.utils.SecurityUtils;
import com.xc.erp.modules.erp.domain.AjaxResult;
import com.xc.erp.modules.erp.domain.dto.SupplierNameParam;
import com.xc.erp.modules.erp.domain.dto.SupplierParam;
import com.xc.erp.modules.erp.domain.pojo.JshSupplier;
import com.xc.erp.modules.erp.service.JshSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * 供应商/客户信息
 *
 * @author wjw 2022/12/9
 * @DateTime: 2022/12/9 22:06
 */
@Api(tags = "会员接口",value = "member接口")
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private JshSupplierService jshSupplierService;

    @ApiOperation("查询会员所有信息")
    @GetMapping("list/{page}/{limit}")
    public AjaxResult list(@PathVariable Integer page,
                           @PathVariable Integer limit,
                           SupplierParam supplierParam){
        IPage<JshSupplier> supplierIPage = jshSupplierService.selectSupplierPage(supplierParam, page, limit);
        return AjaxResult.success(supplierIPage);
    }

    @Log(modul = "会员",desc = "添加会员信息")
    @ApiOperation("新增会员信息")
    @PostMapping("add")
    public AjaxResult add(@RequestBody JshSupplier jshSupplier){
        jshSupplier.setEnabled(true);
        jshSupplier.setTenantId(SecurityUtils.getLoginUser().getJshUser().getTenantId());
        if (jshSupplierService.save(jshSupplier)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @ApiOperation("检查会员是否已存在")
    @GetMapping("checkIsNameAndTypeExist")
    public AjaxResult checkIsNameAndTypeExist(SupplierNameParam supplierNameParam){
        boolean status = jshSupplierService.checkIsNameAndTypeExist(supplierNameParam);
        return AjaxResult.success().put("status",status);
    }

    @Log(modul = "会员",desc = "修改会员信息")
    @ApiOperation("修改会员信息")
    @PutMapping("update")
    public AjaxResult edit(@RequestBody JshSupplier jshSupplier){
        if(jshSupplierService.updateById(jshSupplier)){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @Log(modul = "会员",desc = "删除会员信息")
    @ApiOperation("删除会员信息")
    @DeleteMapping("delete/{id}")
    public AjaxResult deleteById(@PathVariable Long id){
        jshSupplierService.removeById(id);
        return AjaxResult.success();
    }

    @Log(modul = "会员",desc = "批量删除会员信息")
    @ApiOperation("批量删除")
    @DeleteMapping("deleteBatch")
    public AjaxResult deleteBatch(@RequestBody Long[] ids){
        jshSupplierService.removeBatchByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    @Log(modul = "会员",desc = "批量修改会员状态")
    @ApiOperation("批量修改状态")
    @PutMapping("batchSetStatus/{status}")
    public AjaxResult batchSetStatus(@PathVariable boolean status,@RequestBody Long[] ids){
       jshSupplierService.batchSetStatus(status,ids);
        return AjaxResult.success();
    }

    @Log(modul = "会员",desc = "导出会员数据")
    @ApiOperation("导出会员数据")
    @GetMapping("exportExcel")
    public void exportExcel(SupplierParam supplierParam, HttpServletResponse response){
        jshSupplierService.exportExcel(supplierParam,response);
    }

    @Log(modul = "会员",desc = "导入会员数据")
    @ApiOperation("导入会员数据")
    @PostMapping("importExcel")
    public AjaxResult importExcel(MultipartFile file){
        jshSupplierService.importExcel(file);
        return AjaxResult.success();
    }
}
